package com.lottery.controller;

import java.io.BufferedReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.lottery.beans.MemberInfo;
import net.sf.json.JSONObject;

@Controller
public class MemberController {

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject reqObj = getJson(request);
		String memberNo = reqObj.getString("memberNo");
		HttpSession session = request.getSession();

		MemberInfo info = new MemberInfo();
		info.setMemberNo(memberNo);

		session.setAttribute("member", info);
		session.setAttribute("port", request.getLocalPort());

		return "login";
	}
	
	@RequestMapping(value = "/getMemeber.do")
	public String getMemeberInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();

		MemberInfo info=(MemberInfo) session.getAttribute("member");
		
		System.out.println(info.getMemberNo());
		System.out.println("当前端口："+request.getLocalPort());
		System.out.println("session端口："+session.getAttribute("port"));

		return "getMemeber";
	}
	

	@SuppressWarnings("rawtypes")
	private JSONObject getJson(HttpServletRequest request) throws Exception {
		String userStr = readJSONString(request);
		JSONObject requestJsonObj = null;
		Map requestMap = request.getParameterMap();
		// do get 方式提交
		if ((userStr == null || "".equals(userStr)) && requestMap != null && requestMap.size() > 0) {
			requestJsonObj = new JSONObject();
			Set keySet = requestMap.keySet();
			Iterator it = keySet.iterator();
			while (it.hasNext()) {
				String keyName = (String) it.next();
				requestJsonObj.put(keyName, request.getParameter(keyName));
			}
		} else if (userStr != null && !"".equals(userStr)) {

			// Json 格式提交
			if (userStr.startsWith("{")) {
				requestJsonObj = JSONObject.fromObject(userStr);
			} else {
				// key=value方式提交
				requestJsonObj = new JSONObject();
				String reqStrArr[] = userStr.split("&");
				for (int i = 0; i < reqStrArr.length; i++) {
					String kv = reqStrArr[i];
					String kvArr[] = kv.split("=");
					if (kvArr.length > 1) {
						String value = unescape(java.net.URLDecoder.decode(kvArr[1].toString(), "UTF-8"));
						requestJsonObj.put(kvArr[0], value);
					} else {
						requestJsonObj.put(kvArr[0], "");
					}

				}
			}

		}
		return requestJsonObj;
	}

	private static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(src.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}

	private String readJSONString(HttpServletRequest request) {
		StringBuffer json = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = request.getReader();
			while ((line = reader.readLine()) != null) {
				json.append(line);
			}
		} catch (Exception ie) {
		}
		return json.toString();
	}

}
